1479A - Searching Local Minimum - CodeForces Solution


binary search interactive ternary search *1700

Please click on ads to support us..

C++ Code:

#include <iostream>
#include <climits>

using namespace std;

#define N 114514

int l, r;
int n;
int mid;
int t1, t2, t3;
int mem[N];

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin >> n;
    mem[0]=mem[n+1]=INT_MAX;
    l = 0;
    r = n + 1;
    while (r > l) {
        mid = l + r >> 1;
        for (int i = -1; i < 2; ++i) {
            if (!mem[mid + i]) {
                cout << "? " << mid + i << endl;
                cout.flush();
                cin >> t1;
                mem[mid+i]=t1;
            }
        }
        t1=mem[mid-1];
        t2=mem[mid];
        t3=mem[mid+1];
        if (t2 < t1 && t2 < t3) {
            cout << "! " << mid << endl;
            return 0;
        } else if (t1 < t2) {
            r = mid;
        } else {
            l = mid + 1;
        }
    }

    return 0;
}


Comments

Submit
0 Comments
More Questions

450A - Jzzhu and Children
546A - Soldier and Bananas
32B - Borze
1651B - Prove Him Wrong
381A - Sereja and Dima
41A - Translation
1559A - Mocha and Math
832A - Sasha and Sticks
292B - Network Topology
1339A - Filling Diamonds
910A - The Way to Home
617A - Elephant
48A - Rock-paper-scissors
294A - Shaass and Oskols
1213A - Chips Moving
490A - Team Olympiad
233A - Perfect Permutation
1360A - Minimal Square
467A - George and Accommodation
893C - Rumor
227B - Effective Approach
1534B - Histogram Ugliness
1611B - Team Composition Programmers and Mathematicians
110A - Nearly Lucky Number
1220B - Multiplication Table
1644A - Doors and Keys
1644B - Anti-Fibonacci Permutation
1610A - Anti Light's Cell Guessing
349B - Color the Fence
144A - Arrival of the General